package wanba.wanbatv.com.wudao.api;

import android.util.SparseArray;

/**
 * Created by Forcs on 15/7/1.
 */
public class ApiHostFactory {

    private static ApiHostFactory mFactoryInstance = null;

    private static SparseArray<ApiHost> sApiHostMap;

    static {
        //从ConfigUtils类中取出各个渠道的API主机地址
        sApiHostMap = new SparseArray<>();
        final int length = ConfigUtils.API_HOST.length;
        for (int i = 0; i < length; i++) {
            ConfigUtils.ApiHostConfigInfo configInfo = (ConfigUtils.ApiHostConfigInfo) ConfigUtils.API_HOST[i];
            sApiHostMap.put(configInfo.channelId, new ApiHost(configInfo.ip, configInfo.port));
        }
    }

    private ApiHostFactory() {
    }

    public static ApiHostFactory getInstance() {
        if (mFactoryInstance == null) {
            mFactoryInstance = new ApiHostFactory();
        }

        return mFactoryInstance;
    }

    /**
     * 根据渠道号获取渠道主机地址字符串
     * @param channelId 渠道号
     * @return 主机地址字符串，[ip]:[port]
     */
    public String getApiHostString(int channelId) {
        ApiHost apiHost = getApiHost(channelId);
        if (apiHost != null) {
            return apiHost.toString();
        }

        return null;
    }

    /**
     * 根据渠道号获取渠道主机信息对象
     * @param channelId 渠道号
     * @return 渠道主机信息对象
     */
    public ApiHost getApiHost(int channelId) {
        return sApiHostMap.get(channelId);
    }
}
